package com.hsylient.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
@Slf4j(topic = "springLog")
public class UserMapperImplProxy {

    /**
     * 抽离出来一个切点
     * 这个切点表达式写接口和写实现类都可以
     * execution(* com.hsylient.service.impl.UserServiceImpl.tagMethod(..)
     * execution(* com.hsylient.service.UserService.tagMethod(..)
     */
    @Pointcut("execution(* com.hsylient.service.UserService.tagMethod(..))")
    public void myPointcut() {
    }

    @Before("myPointcut()")
    public void before() {
        log.info("tagMethod()方法之前执行");
    }

    @After("myPointcut()")
    public void after() {
        log.info("tagMethod()方法之后执行(一定执行)");
    }

    @AfterReturning("myPointcut()")
    public void afterReturning() {
        log.info("tagMethod()方法之后执行");
    }

    @Around("myPointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        log.info("tagMethod()方法前环绕");
        System.out.println(Arrays.toString(pjp.getArgs()));
        Object proceed = pjp.proceed();
        log.info("tagMethod()方法后环绕");
        return proceed;
    }

    @AfterThrowing("myPointcut()")
    public void afterThrowing() {
        log.info("tagMethod()方法发生异常的时候执行");
    }
}
